/**
 * Created by Delicate on 2017/5/31.
 */
var fairList;
$(document).ready(function(){
	init();
    monitor();
    service();
    page();
})

function init(){
    $("#province").trigger("select");
    load(1);
    $("#jumpVal").keydown(function(e) {  
        if (e.keyCode == 13) {  
              jump();
        }  
   });
}
function monitor(){
    $(".m-select-search").on("click",function(res){
        load(1);
    })
}
function service(){

}
var totalPage=1;
function load(number){
    $.ajax({
        type:"post",
        url:"/platform/fair/list",
        datatype:"json",
        data:{
            pageNum: number,
            pageSize: 12,
            nickName: $("#nickName").val(),
            channel: $("#channel").val(),
            city: $("#city").val(),
            province: $("#province").val(),
            platform: $("#platform").val(),
            feature: $("#feature").val(),
            "price": $("#price").val()
        },
        success:function(data){
            var _html = "";
            if(data.list.length>0){
                for (var i=0;i<data.list.length;i++){
                    var item = data.list[i];
                    _html += '<li class="m-choice-cell" style="height:215px;" onclick="cellClick(this)" data-id="'+item.id+'" data-head="'+item.photo+'" data-name="'+item.nick_name+'"> ' +
                        '<div class="u-cell-item-head"> ' +
                        '<div class="u-item-head" style="background: url('+item.photo+') center/cover no-repeat"></div> ' +
                        '<div class="u-item-name" style="font-size:16px;margin-top:10px;">'+item.nick_name+'</div> ' +
                        '<div class="u-item-summary">'+item.attrString+'</div> ' +
                        '</div> ' +
                        '<div class="u-cell-item-number"> ' +
                        '<div class="u-item-yxl"> ' +
                        '<div class="u-item-icon"></div> ' +
                        '<div class="u-item-text">点击数 <span style="margin-left:2px;">'+item.click+'</span></div> ' +
                        '</div> ' +
                        '<div class="u-item-rate"> ' +
                        '<div class="u-item-icon" style="background: url(/assets/front/web/resources/icon-zh.png) center/contain no-repeat;"></div> ' +
                        '<div class="u-item-text">转化数 <span style="margin-left:2px;">'+item.cvs+'</span></div> ' +
                        '</div> ' +
                        '</div> ' +
                        '</li>';
                }
                page(data.totalPage,data.pageNumber);
                if(data.totalPage>1){
                	$("#m_choice_page_container").show();
                }else{
                	$("#m_choice_page_container").hide();
                }
                
            }else {
//            	layer.alert("没有检索到任何相关信息");
            	_html+='<div style="margin:0 auto;padding:15px;color:red">暂时没有相关数据信息</div>'
            	$("#m_choice_page_container").hide();
            }
            $(".m-choice-list").html(_html);
        }
    })
}

function page(total,index){
	totalPage =total;
    laypage({
        cont: 'm_choice_page', //容器。值支持id名、原生dom对象，jquery对象。【如该容器为】：<div id="page1"></div>
        pages: total?total:1, //通过后台拿到的总页数
        curr: index || 1, //当前页
        jump: function(obj, first){ //触发分页后的回调
            if(!first){ //点击跳页触发函数自身，并传递当前页：obj.curr
                load(obj.curr);
            }
        }
    });
}

function jump(){
	var jumpVal=$("#jumpVal").val();
	var re = /^[0-9]+.?[0-9]*$/;
	if (!re.test(jumpVal)) {
		layer.alert("请输入正确的整数");
        return;
	}else if(jumpVal<=0){
		layer.alert("请输入大于1的整数");
        return;
	}else if(jumpVal>totalPage){
		layer.alert("已经超出最大页码数了");
        return;
	}else if(jumpVal&&jumpVal<=totalPage){
		load(jumpVal);
	}
}

//选择省 生成对应市
function changeRegion(obj){
    var val = $(obj).val();
    if(val==""){
    	$("#city").empty();
        $("#city").append("<option value=''>请选择</option>")
        return;
    }
    
    $.ajax({
        type: "post",
        url: ctx + "/platform/fair/region",
        datatype: "json",
        data: {"id": val},
        success: function (data) {
            if (data.length > 0) {
                $("#city").empty();
                $("#city").append("<option value=''>请选择</option>")
                for (var i = 0; i < data.length; i++) {
                    $("#city").append("<option value=" + data[i].REGION_ID + " class=''>" + data[i].REGION_NAME + "</option>")
                }
            }
        }
    })

}

function cellClick(obj){
    const id = $(obj).data('id');
    window.location.href = "/platform/fair/detail?id="+id;
}

function clearSearch(){
	$("#nickName").val("");
}